#################################################################################### 
####################################################################################
####################################################################################
# Appendix D: Tables 8--11
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
library(RItools)
####################################################################################
####################################################################################
## Read in survey data (from: analysis_villages.R)
dat = readRDS(here("data/village_d811a.rds"))
data = readRDS(here("data/village_d811b.rds"))

####################################################################################
####################################################################################
## Table D.8

## Control group balance
# Years Access
model = formula("treat ~ ngo_count + hf_count + s_count + satisfaction_vht + grid_access_years + 
                h2o_access_years + main_h2o_source + sewa_access_years + road_make")
balance_test = xBalance(model,
                        strata = factor(dat$district_name), #list(noblocks=NULL), cluster = factor(dat$village_id), 
                        data = dat, na.rm = T,
                        report=c("adj.means","adj.mean.diffs","z.scores","chisquare.test","p.values"))
rownames(balance_test$results) = c("NGO Count","Health Facility Count","School Count", "LC1 Satisfaction w/VHT","Years of Grid Access",
                                   "Years of Piped Water", "Main Water Source", "Years of Sewage Access", "Road Material")
rownames(balance_test$overall) = c("Overall Test Statistic")


kable(round(balance_test$results,2),align = "c", booktabs = T, caption = "Village-Level Block-Adjusted Omnibus Balance Test (Original Study Control)", 
      col.names = c("Control Mean", "Treatment Mean", "Difference","Z-score","P-value"), format = "latex")
kable(round(balance_test$overall,2), align = "c", row.names = T,
      col.names = c("Chi-Sq", "Df", "P-value"), format = "latex")

####################################################################################
####################################################################################
## Table D.10

# Binary Access
model = formula("treat ~ ngo_count + hf_count + s_count + satisfaction_vht + electricity + main_h2o_source + sewa + road_make")
balance_test = xBalance(model,
                        strata = factor(dat$district_name), #list(noblocks=NULL), cluster = factor(dat$village_id), 
                        data = dat, na.rm = T,
                        report=c("adj.means","adj.mean.diffs","z.scores","chisquare.test","p.values"))
rownames(balance_test$results) = c("NGO Count","Health Facility Count","School Count", "LC1 Satisfaction w/VHT","Grid Access",
                                   "Main Water Source", "Sewage Access", "Road Material")
rownames(balance_test$overall) = c("Overall Test Statistic")

kable(round(balance_test$results,2),align = "c", booktabs = T, caption = "Village-Level Block-Adjusted Omnibus Balance Test (Original Study Control)", 
      col.names = c("Control Mean", "Treatment Mean", "Difference","Z-score","P-value"), format = "latex")
kable(round(balance_test$overall,2), align = "c", row.names = T,
      col.names = c("Chi-Sq", "Df", "P-value"), format = "latex")




####################################################################################
####################################################################################
## Table D.9

## Balance in Sample villages (All treated vs pure control)

## Sample Balance
# Access Years
model = formula("treat ~ ngo_count + hf_count + s_count + satisfaction_vht + grid_access_years + 
                h2o_access_years + main_h2o_source + sewa_access_years + road_make")
balance_test = xBalance(model,
                        strata = factor(data$district_name), #list(noblocks=NULL), cluster = factor(dat$village_id), 
                        data = data, na.rm = T,
                        report=c("adj.means","adj.mean.diffs","z.scores","chisquare.test","p.values"))

rownames(balance_test$results) = c("NGO Count","Health Facility Count","School Count", "LC1 Satisfaction w/VHT","Years of Grid Access",
                                   "Years of Piped Water", "Main Water Source", "Years of Sewage Access", "Road Material")
rownames(balance_test$overall) = c("Overall Test Statistic")

kable(round(balance_test$results,2),align = "c", booktabs = T, caption = "Village-Level Block-Adjusted Omnibus Balance Test (Full Sample)", 
      col.names = c("Control Mean", "Treatment Mean", "Difference","Z-score","P-value"), format = "latex")
kable(round(balance_test$overall,2), align = "c", row.names = T,
      col.names = c("Chi-Sq", "Df", "P-value"), format = "latex")

####################################################################################
####################################################################################
## Table D.11

# Binary Access
model = formula("treat ~ ngo_count + hf_count + s_count + satisfaction_vht + electricity + main_h2o_source + sewa + road_make")
balance_test = xBalance(model,
                        strata = factor(data$district_name), #list(noblocks=NULL), cluster = factor(dat$village_id), 
                        data = data, na.rm = T,
                        report=c("adj.means","adj.mean.diffs","z.scores","chisquare.test","p.values"))
rownames(balance_test$results) = c("NGO Count","Health Facility Count","School Count", "LC1 Satisfaction w/VHT","Grid Access",
                                   "Main Water Source", "Sewage Access", "Road Material")
rownames(balance_test$overall) = c("Overall Test Statistic")

kable(round(balance_test$results,2),align = "c", booktabs = T, caption = "Village-Level Block-Adjusted Omnibus Balance Test (Full Sample)", 
      col.names = c("Control Mean", "Treatment Mean", "Difference","Z-score","P-value"), format = "latex")
kable(round(balance_test$overall,2), align = "c", row.names = T,
      col.names = c("Chi-Sq", "Df", "P-value"), format = "latex")

####################################################################################

